/**
 * 
 */
package com.android.gtdroid;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

/**
 * @author xlees
 *
 */
public class ViewTaskAct extends Activity {
	private ArrayList<HashMap<String, Object>> groups = new ArrayList<HashMap<String, Object>>();
	private ArrayList<ArrayList<HashMap<String, Object>>> children =
			new ArrayList<ArrayList<HashMap<String, Object>>>();
//	private ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
	private MySimpleExpandableListAdapter msela;
	
	private ExpandableListView task_view;
	private long posInTable;
	private int listIndex;
	private HashMap<String, Object> hmSelected;
	private GTDApp ga;
	private int taskStatus;
	private Button btn_new;
	private int sortIndex;		// current sort type
	
	private final String tag = "ViewTaskAct";
	private final String[] fromCols = new String[] {
			"item_name",
			"item_context",
			"item_pos",
			"item_tags"};
	private final int[] toLayouts = new int[] {
			R.id.tv_task,
			R.id.tv_row_context,
			R.id.tv_row_pos,
			R.id.tv_row_tag};
	
	private final int EDIT_TASK = Menu.FIRST;
	private final int MOVE_TASK = Menu.FIRST+1;
	private final int REMOVE_TASK = Menu.FIRST+2;
	private Calendar calCur;
	
	
	public int getSortIndex() {
		return sortIndex;
	}
	
	public ExpandableListView getListView() {
		return task_view;
	}
	
	public int getListIndex() {
		return listIndex;
	}
	
	public int getTaskStatus() {
		return taskStatus;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Log.i(tag, "onCreate Method is called!");
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.task_view);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_cmn);
		
		btn_new = ((Button) findViewById(R.id.btn_new));
		task_view = (ExpandableListView) findViewById(R.id.elv_tasks);
		
		ga = (GTDApp) getApplication();
		calCur = Calendar.getInstance();
//		sortIndex = new ArrayList<Integer>();
		
		
		taskStatus = getIntent().getIntExtra("task_status", C.TASK.UNCOMPLETED);
		listIndex = getIntent().getIntExtra("list_index", 0);
		
		restoreSortType(taskStatus, listIndex);
		
		String title;
		if (taskStatus == C.TASK.UNCOMPLETED) {
			title = ga.lists.get(listIndex);
		}
		else if (taskStatus == C.TASK.CANCELLED) {
			title = new String("垃圾箱");
			btn_new.setText("清空");
		}
		else {			// completed task
			title = new String("已完成");
			btn_new.setVisibility(View.INVISIBLE);
		}
		((TextView) findViewById(R.id.tv_title)).setText(title);

		msela = new MySimpleExpandableListAdapter(
				this,
				C.ADAPTER.VIEWTASK,
				groups,
				R.layout.group,
				new String[] { "group_name" },
				new int[] { R.id.tv_group },
				children,
				R.layout.row_task,
				fromCols,
				toLayouts);
		task_view.setAdapter(msela);
		updateAdapterData();
		
		processTaskView();
		
		registerForContextMenu(task_view);
	}
	
	private void restoreSortType(int _taskStatus, int _listIndex) {
		if (_taskStatus == C.TASK.COMPLETED) {	// completed
			sortIndex = ga.sortType.get("completed");
		}
		else if (_taskStatus == C.TASK.CANCELLED) {  // trash
			sortIndex = ga.sortType.get("trash");
		}
		else {		// uncompleted
			switch (_listIndex) {
				case 0: // inbox
					sortIndex = ga.sortType.get("inbox");
					break;
					
				case 1: // next
					sortIndex = ga.sortType.get("next");
					break;
					
				case 2:		// agenda
					sortIndex = ga.sortType.get("agenda");
					break;
					
				case 3:		// waiting
					sortIndex = ga.sortType.get("waiting");
					break;
					
				case 4:		// memo
					sortIndex = ga.sortType.get("memo");
					break;
					
				default:
					break;
			}
		}
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		
		updateAdapterData();
	}
	
	private void updateAdapterData() {
		ArrayList<HashMap<String,Object>> items = null;
		HashMap<String, Object> hm;
		Cursor taskCursor = null;
		String sql = null;
		ArrayList<String> al = null;
		String list = "", where = "";
		int size;
		int[] index;
		String sortName = "";
		
		groups.clear();
		for (int i=0; i<children.size(); i++)
			children.get(i).clear();
		children.clear();
		
		if (listIndex != -1)
			list = ga.lists.get(listIndex);
		switch (sortIndex) {
			case 0:		// time(default)
				hm = new HashMap<String, Object>();
				hm.put("group_name", "按时间降序");
				groups.add(hm);
				
				if (taskStatus == C.TASK.COMPLETED) {		// completed task list
					sql = "select * from " + ga.getDbta().getTableName()
						+ " where " + DBTaskAdapter.KEY_COMPLETED + "=" + C.TASK.COMPLETED
						+ " order by task_created desc";
				}
				else if (taskStatus == C.TASK.CANCELLED) {		// trash task list
					sql = "select * from " + ga.getDbta().getTableName()
						+ " where " + DBTaskAdapter.KEY_COMPLETED + "=" + C.TASK.CANCELLED
						+ " order by task_created desc";
				}
				else {		// uncompleted
					sql = "select * from " + ga.getDbta().getTableName()
						+ " where " + DBTaskAdapter.KEY_LIST + "=\"" + list
						+"\" AND "+DBTaskAdapter.KEY_COMPLETED + "=" + C.TASK.UNCOMPLETED
						+ " order by task_created desc";
				}
				taskCursor = ga.getDbta().excuteSQL(sql);
				
				items = new ArrayList<HashMap<String,Object>>();
				fillArray(taskCursor, items);
				children.add(items);
				
				task_view.expandGroup(0);		// fix bug(empty layout)
				
				break;
				
			case 1:		// context
				al = ga.getContexts();
				sortName = DBTaskAdapter.KEY_CONTEXT;
				
				break;
				
			case 2:		// position
				al = ga.getPositions();
				sortName = DBTaskAdapter.KEY_POS;
				
				break;
				
			case 3:		// project
				al = ga.getProjects();
				sortName = DBTaskAdapter.KEY_PROJECT;
				
				break;
				
			case 4:		// priority
				al = ga.prior;
				sortName = DBTaskAdapter.KEY_PRIOR;
				
				break;
				
			default:
				break;
		}
		
		if (sortIndex > 0) {
			size = al.size();
			index = new int[size];
			for (int i=0; i<size-1; i++)
				index[i] = i+1;
			index[size-1] = 0;
			
			for (int i : index) {
				if (taskStatus == C.TASK.UNCOMPLETED) {
					sql = "select * from " + ga.getDbta().getTableName()
							+ " where " + sortName + "=\"" + al.get(i)
							+ "\" AND " + DBTaskAdapter.KEY_COMPLETED + "=" + C.TASK.UNCOMPLETED
							+ " AND " + DBTaskAdapter.KEY_LIST + "=\"" + list + "\""
							+ " order by task_created desc";
					
					/*where = sortName +"=\"" + al.get(i)
							+ "\" AND "+DBTaskAdapter.KEY_COMPLETED+"="+C.TASK.UNCOMPLETED
							+ " AND "+DBTaskAdapter.KEY_LIST+"=\""+list+"\"";*/
				}
				else if (taskStatus == C.TASK.COMPLETED) {
					sql = "select * from " + ga.getDbta().getTableName()
							+ " where " + sortName + "=\"" + al.get(i)
							+ "\" AND " + DBTaskAdapter.KEY_COMPLETED + "=" + C.TASK.COMPLETED
							+ " order by task_created desc";
					
					/*where = sortName +"=\"" + al.get(i)
							+ "\" AND "+DBTaskAdapter.KEY_COMPLETED+"="+C.TASK.COMPLETED;*/
				}
				else {		// trash
					sql = "select * from " + ga.getDbta().getTableName()
							+ " where " + sortName + "=\"" + al.get(i)
							+ "\" AND " + DBTaskAdapter.KEY_COMPLETED + "=" + C.TASK.CANCELLED
							+ " order by task_created desc";
					
					/*where = sortName +"=\"" + al.get(i)
							+ "\" AND "+DBTaskAdapter.KEY_COMPLETED+"="+C.TASK.CANCELLED;*/
				}
				taskCursor = ga.getDbta().excuteSQL(sql);
				if (taskCursor.getCount() == 0)
					continue;
				
				hm = new HashMap<String, Object>();
				hm.put("group_name", al.get(i));
				groups.add(hm);
				
				Log.e(tag, "number of context "+al.get(i)+": "+taskCursor.getCount());
				
				items = new ArrayList<HashMap<String, Object>>();
				fillArray(taskCursor, items);
				children.add(items);
			}
			
			for (int i=0; i<groups.size(); i++)
				task_view.expandGroup(i);
		}
		
		msela.notifyDataSetChanged();
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
//		updateAdapterData();
	}
	
	/**
	 * fill array $item with the specific condition
	 * @param taskCursor
	 */
	private void fillArray(Cursor taskCursor, ArrayList<HashMap<String, Object>> items) {
		HashMap<String, Object> hm;
		
		if (taskCursor.moveToFirst()) {
			do {
				long rowId = taskCursor.getLong(DBTaskAdapter.COL_ID);
				String task = taskCursor.getString(DBTaskAdapter.COL_TASK_NAME);
				String note = taskCursor.getString(DBTaskAdapter.COL_TASK_NOTE);
				String list = taskCursor.getString(DBTaskAdapter.COL_TASK_LIST);
				
				String ctt = taskCursor.getString(DBTaskAdapter.COL_TASK_CONTEXT);
				String prj = taskCursor.getString(DBTaskAdapter.COL_TASK_PROJECT);
				String pos = taskCursor.getString(DBTaskAdapter.COL_TASK_POS);
				
				int status = taskCursor.getInt(DBTaskAdapter.COL_TASK_STATUS);
				String st = taskCursor.getString(DBTaskAdapter.COL_TASK_ST);
				String et = taskCursor.getString(DBTaskAdapter.COL_TASK_ET);
				String repeat = taskCursor.getString(DBTaskAdapter.COL_TASK_REPEAT);
				String remind = taskCursor.getString(DBTaskAdapter.COL_TASK_REMIND);
				
				String prior = taskCursor.getString(DBTaskAdapter.COL_TASK_PRIOR);
				String tags = taskCursor.getString(DBTaskAdapter.COL_TASK_TAG);
				String created = taskCursor.getString(DBTaskAdapter.COL_TASK_CREATED);
				int completed = taskCursor.getInt(DBTaskAdapter.COL_TASK_COMPLETED);
				
				
				hm = new HashMap<String, Object>();
				hm.put("item_id", rowId);
				hm.put("item_name", task);
				hm.put("item_note", note);
				hm.put("item_list", list);
				
				hm.put("item_context", ctt);
				hm.put("item_project", prj);
				hm.put("item_pos", pos);
				
				hm.put("item_status", status);
				hm.put("item_st", st);
				hm.put("item_et", et);
				hm.put("item_repeat", repeat);
				hm.put("item_remind", remind);
				
				hm.put("item_prior", prior);
				hm.put("item_tags", tags);
				hm.put("item_created", created);
				hm.put("item_completed", completed);
				
				items.add(hm);
			} while (taskCursor.moveToNext());
		}
	}
	
	/*public void updateAdapterData() {
		Cursor taskCursor;
		
		items.clear();
		
		if (taskStatus == C.TASK.COMPLETED) {		// completed task list
			taskCursor = ga.getDbta().fetchTasks(DBTaskAdapter.KEY_COMPLETED+"="+C.TASK.COMPLETED);
		} else if (taskStatus == C.TASK.CANCELLED) {		// trash task list
			taskCursor = ga.getDbta().fetchTasks(DBTaskAdapter.KEY_COMPLETED+"="+C.TASK.CANCELLED);
		} else {
			String list = ga.lists.get(listIndex);
			String where = DBTaskAdapter.KEY_LIST+"=\""+list
					+"\" AND "+DBTaskAdapter.KEY_COMPLETED+"="+C.TASK.UNCOMPLETED;
			taskCursor = ga.getDbta().fetchTasks(where);
		}
		fillArray(taskCursor, items);
		
		msela.notifyDataSetChanged();
	}*/
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		ExpandableListView.ExpandableListContextMenuInfo info =
				(ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
//		int group = ExpandableListView.getPackedPositionGroup(info.packedPosition);
//		int child = ExpandableListView.getPackedPositionChild(info.packedPosition);
		
		//Only create a context menu for child items
		if (type == 1) {
			menu.setHeaderTitle("编辑任务");
			
			if (taskStatus != C.TASK.CANCELLED) {
				menu.add(0, EDIT_TASK, 1, "编辑");
				menu.add(0, REMOVE_TASK, 2, "删除");
			}
			menu.add(0, MOVE_TASK, 0, "移动到");
		}
		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
		
		int type, groupPos = 0, childPos = 0;
		
		ExpandableListContextMenuInfo info =
				(ExpandableListContextMenuInfo) item.getMenuInfo();
		type = ExpandableListView.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
			groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
		}
		
//		Toast.makeText(ViewTaskAct.this, "group: "+groupPos+" child: "+childPos, Toast.LENGTH_LONG);
		Log.e(tag, "group: "+groupPos+" child: "+childPos+" clicked!");
		
		hmSelected = (HashMap<String, Object>) children.get(groupPos).get(childPos);
		posInTable = (Long) hmSelected.get("item_id");
		
		if (taskStatus != C.TASK.UNCOMPLETED) {
			String list = (String) hmSelected.get("item_list");
			listIndex = ga.lists.indexOf(list);
		}

		
		switch (item.getItemId()) {
			case EDIT_TASK:
				Intent itt = new Intent(ViewTaskAct.this, NewTaskAct.class);
				
				itt.putExtra("task_data", hmSelected);
				itt.putExtra("list_index", listIndex);
				itt.putExtra("task_status", taskStatus);
				
				startActivityForResult(itt, 0);
				return true;
				
			case MOVE_TASK:
				ArrayList<String> als = new ArrayList<String>();
				ArrayList<ListItem> al = ga.getListItems();
				
				for (int i=0; i<al.size(); i++)
					als.add(al.get(i).getListName());
				final String[] listItems = (String[]) als.toArray(new String[als.size()]);
				
				AlertDialog.Builder builder = new AlertDialog.Builder(ViewTaskAct.this);
				builder.setTitle("移动到");
				builder.setSingleChoiceItems(listItems, listIndex, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int item) {
						// TODO Auto-generated method stub
						if (item != listIndex) {
							TaskItem ti = ga.getDbta().getTaskItem(posInTable);
							ti.setListName(listItems[item]);
							ti.setCompletedStatus(C.TASK.UNCOMPLETED);
							
							/*if (listIndex == GTDApp.COMPLETED
									|| listIndex == GTDApp.TRASH) {
								ti.setCompletedStatus(TaskItem.UNCOMPLETED);
							}*/
							
							if (item == GTDApp.AGENDA) {	// move a non-agenda task to agenda task
								ti.setAllDay(true);
								ti.setStartTime((new SimpleDateFormat("yyyy-MM-dd")).format(calCur.getTime()));
								ti.setEndTime((new SimpleDateFormat("yyyy-MM-dd")).format(calCur.getTime()));	// no deadline
								ti.setRepeat("");	// no repeat
							}
							ga.getDbta().updateTask(posInTable, ti);
//							updateAdapterData();
							updateAdapterData();
						}
						dialog.dismiss();
					}
				});
				builder.show();
				return true;
			
			case REMOVE_TASK: // remove a task
//				ga.getDbta().removeTask(posInTable);
				TaskItem ti = ga.getDbta().getTaskItem(posInTable);
				ti.setCompletedStatus(C.TASK.CANCELLED);
				ga.getDbta().updateTask(posInTable, ti);
//				updateAdapterData();
	//			ga.updateTask();
				updateAdapterData();
				
				return true;
			default:
				break;
		}
		
		return false;
	}
	
	private void processTaskView() {
		task_view.setGroupIndicator(null);
		for (int i=0; i<groups.size(); i++)
			task_view.expandGroup(i);
		
		if (taskStatus == C.TASK.COMPLETED) {		// completed
			btn_new.setVisibility(View.INVISIBLE);
		}
		else if (taskStatus == C.TASK.CANCELLED) {		// trash
			btn_new.setText("清空");
			
			boolean isEmpty = true;
			for (int i=0; i<groups.size(); i++) {
				if (children.get(i).size() > 0) {
					isEmpty = false;
					break;
				}
			}
			if (isEmpty)	{	// no trash
				btn_new.setEnabled(false);
			}
			
			btn_new.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog.Builder builder = new AlertDialog.Builder(ViewTaskAct.this);
					builder.setTitle("是否要永久清空\"垃圾箱\"中的所有任务？");
					
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							for (int i=0; i<groups.size(); i++) {
								for (int j=0; j<children.get(i).size(); j++) {
									long rowId = (Long) children.get(i).get(j).get("item_id");
									ga.getDbta().removeTask(rowId);
								}
							}
							
							updateAdapterData();
							
							btn_new.setEnabled(false);
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.dismiss();
						}
					});
					builder.show();
				}
			});
		}
		else {		// uncompleted task
			btn_new.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent itt = new Intent(ViewTaskAct.this, NewTaskAct.class);
					itt.putExtra("list_index", listIndex);
					
					startActivityForResult(itt, 0);
				}
			});
		}
		
		Button btn_return = ((Button) findViewById(R.id.btn_return));
		btn_return.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		/*OnItemLongClickListener oilsl = new OnItemLongClickListener() {
			@SuppressWarnings("unchecked")
			public boolean onItemLongClick(AdapterView<?> parent, View v,
					int pos, long id) {
				// TODO Auto-generated method stub
				hmSelected = (HashMap<String, Object>) task_view.get(groupPosition).get(childPosition);
				posInTable = (Long) hmSelected.get("item_id");
				
				if (taskStatus != C.TASK.UNCOMPLETED) {
					String list = (String) hmSelected.get("item_list");
					listIndex = ga.lists.indexOf(list);
				}
				
				Log.e(tag, "listIndex is: "+listIndex);
				// setTitle("long click"+current_str);

				return false;
			}
		};*/
		
		OnChildClickListener ocl = new OnChildClickListener() {
			@SuppressWarnings("unchecked")
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				
				hmSelected = (HashMap<String, Object>) children.get(groupPosition).get(childPosition);
				posInTable = (Long) hmSelected.get("item_id");
				
				if (taskStatus != C.TASK.UNCOMPLETED) {
					String list = (String) hmSelected.get("item_list");
					listIndex = ga.lists.indexOf(list);
				}
				Log.e(tag, "listIndex is: "+listIndex);
				
				Intent itt = new Intent(ViewTaskAct.this, TaskDetailAct.class);
				
				itt.putExtra("row_id", posInTable);
				itt.putExtra("selected_task", hmSelected);
				itt.putExtra("list_index", listIndex);
				itt.putExtra("task_status", taskStatus);
				
				startActivity(itt);
				
				return false;
			}
		};
		task_view.setOnChildClickListener(ocl);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		
		MenuItem itemSort = menu.add(0, C.MENU.SORT, 0, "排序");
		MenuItem itemHome = menu.add(0, C.MENU.HOME, 1, "主页");
		
//		itemSort.setIcon(R.drawable.add_new_item);
		
		return true;
	}
	
	private void saveSortType(int _taskStatus, int _listIndex, int _sortType) {
		Editor edit = ga.getSpSortType().edit();
		
		if (_taskStatus == C.TASK.COMPLETED) {	// completed
			ga.sortType.put("completed", _sortType);
			edit.putInt("completed", _sortType);
		}
		else if (_taskStatus == C.TASK.CANCELLED) {  // trash
			ga.sortType.put("trash", _sortType);
			edit.putInt("trash", _sortType);
		}
		else {		// uncompleted
			switch (_listIndex) {
				case 0: 	// inbox
					ga.sortType.put("inbox", _sortType);
					edit.putInt("inbox", _sortType);
					break;
					
				case 1: 	// next
					ga.sortType.put("next", _sortType);
					edit.putInt("next", _sortType);
					break;
					
				case 2:		// agenda
					ga.sortType.put("agenda", _sortType);
					edit.putInt("agenda", _sortType);
					break;
					
				case 3:		// waiting
					ga.sortType.put("waiting", _sortType);
					edit.putInt("waiting", _sortType);
					break;
					
				case 4:		// memo
					ga.sortType.put("memo", _sortType);
					edit.putInt("memo", _sortType);
					break;
					
				default:
					break;
			}
		}

		edit.commit();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId()) {
			case C.MENU.SORT:
				AlertDialog.Builder builder = new AlertDialog.Builder(ViewTaskAct.this);
				builder.setTitle("排序");
				
				final String[] sortItems = (String[]) ga.getSort().toArray(new String[ga.getSort().size()]);
				builder.setSingleChoiceItems(sortItems, sortIndex, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if (which != sortIndex) {
							sortIndex = which;
							
							updateAdapterData();
							saveSortType(taskStatus, listIndex, sortIndex);
						}
						
						dialog.dismiss();
					}
				});
				
				builder.show();
				break;
				
			case C.MENU.HOME:
				break;
				
			default:
				break;
		}
		
		return true;
	}
	
	
}
